<?php
require_once('inc/init.php');
require_once('inc/pager.php');
require_once getCacheFilePath('forums.php');
//允许使用搜索
if(getPopedom(4)==0){
	printMsg('popedom_4');
}

$action = strFilter($_GET["action"]);
$curPage = numFilter($_GET["page"]);
$keyword = htmlFilter($_GET["k"]);
$fid = strFilter($_GET["fid"]);
if(empty($fid) || substr($fid,0,2)=='0,'){
	$fid='0';
}else{
	$fidarr=array();
	foreach(explode(",",$fid) as $fidstr){
		array_push($fidarr, intval($fidstr));
	}
	$fid=implode(",", $fidarr);
}
$area = numFilter($_GET["area"]);
$area=$area==2?2:1;
$topictypestr = strFilter($_GET["topictypestr"]);
$topictypestr = (empty($topictypestr)||$topictypestr=='undefined')?'all':$topictypestr;
$topicspc = strFilter($_GET["topicspc"]);
$topicspc = (empty($topicspc)||$topicspc=='undefined')?'all':$topicspc;
$starttime = strFilter($_GET["starttime"]);
$stoptime = strFilter($_GET["stoptime"]);

$rows=array();
$headtitle="{$keyword} - 论坛搜索";
$pager = new Pager();
$pager->init($cache_settings['topicspernum'],$curPage,"search.php?action={$action}&k={$keyword}&fid={$fid}&area={$area}&page={page}&topictypestr={$topictypestr}&topicspc={$topicspc}&starttime={$starttime}&stoptime={$stoptime}","PAGER",0);

if(empty($action)){
	$cond="id=0";
	$rows=array();
}else{

	//搜索时间间隔
	if(intval($cache_settings['searchitime'])>0 && !empty($keyword)){
		if(intval(getCookies('lastsearchtime'))>time()-$cache_settings['searchitime'] && getCookies('lastkeyword')!=$keyword && $lg['groupid']<200){
			printMsg('search_searchitime');
		}
		setCookies('lastkeyword',$keyword);
		setCookies('lastsearchtime',time());
	}


	if($action=="search"){
		$cond="topics.id=posts.tid and posts.istopic=1 and topics.isrecycle=0";
		if($fid!='0'){
			$cond.=" and posts.fid in ({$fid})";
		}
		if($topictypestr=='normal'){
			$cond.=" and topics.topictype=0";
		}
		if($topictypestr=='vote'){
			$cond.=" and topics.topictype=1";
		}
		if($topictypestr=='reward'){
			$cond.=" and topics.topictype=2";
		}
		if($topicspc=='good'){
			$cond.=" and topics.isgood=1";
		}
		if($topicspc=='top'){
			$cond.=" and (topics.istop=1 or topics.isalltop=1)";
		}
		$starttime_int= empty($starttime) ? 0 : (strtotime($starttime)-$cache_settings['timeoffset']*3600);
		$stoptime_int= empty($stoptime) ? 0 :(strtotime($stoptime)+24*3600-$cache_settings['timeoffset']*3600-1);
		if($starttime_int>0){
			$cond.=" and topics.posttime>=$starttime_int";
		}
		if($stoptime_int>0){
			$cond.=" and topics.posttime<=$stoptime_int";
		}

		if(!empty($keyword)){
			if($area==2 && getPopedom(4)==2){
				$cond.=" and (posts.username like '%{$keyword}%' or posts.title like '%{$keyword}%' or posts.content like '%{$keyword}%')";
			}else{
				$cond.=" and (posts.username like '%{$keyword}%' or posts.title like '%{$keyword}%')";
			}
		}else{
			printMsg('search_nokeyword');
		}
		$rows = $pager->queryRows($db,"topics,posts","{$cond}", "*,topics.id as id","topics.posttime desc,topics.id desc");
	}elseif($action=="good"){
		$headtitle="精华帖子 - 论坛搜索";
		$cond= "isgood=1";
		$rows = $pager->queryRows($db,"topics","{$cond}", "*","id desc");
	}elseif($action=="new"){
		$headtitle="今日新帖- 论坛搜索";
		$last24hour=$_SYS['time']-3600*24;
		$cond= "posttime>{$last24hour}";
		$rows = $pager->queryRows($db,"topics","{$cond}", "*","posttime desc");
	}
}
foreach($rows as $key=>$row){
	$row['posttime']=getDateStr($row['posttime']);
	$forum=$cache_forums[$row['fid']];
	$row['title']=htmlFilter(cutStr($row['title'],50));
	$row['username']=htmlFilter($row['username']);
	if(!empty($row['highlight'])){
		$row['title']=genHighLighStr($row['highlight'],$row['title']);
	}
	$row['icon']=getTopicIcon($row);
	$row['forumurl']=genUrl("forum.php?fid={$row['fid']}");
	$row['viewurl']=genUrl("view.php?tid={$row['id']}");
	$row['userurl']=genUrl("user.php?uid={$row['userid']}");
	$rows[$key]=$row;
}

require_once('header.php');
require_once getTemplatePath('search.htm');
footer();
?>